/**
 * 
 */
package fr.rcp.file.explorer.controleur;

import java.io.File;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.ide.FileStoreEditorInput;

import fr.rcp.file.explorer.views.Console;

/**
 * @author fred
 * 
 */
public class OpenAction extends Action {
	private Console parent;

	public OpenAction(Console parent, String label) {
		this.parent = parent;
		setText(label);
	}

	public void run() {
		IStructuredSelection selection = (IStructuredSelection) parent
				.getTableViewer().getSelection();
		File selected_file = (File) selection.getFirstElement();
		if (selected_file != null) {
			IWorkbenchPage page = PlatformUI.getWorkbench()
					.getActiveWorkbenchWindow().getActivePage();
			try {
				IFileStore fileStore = EFS.getStore(selected_file.toURI());
				IEditorInput input = new FileStoreEditorInput(fileStore);
				page.openEditor(input, EditorsUI.DEFAULT_TEXT_EDITOR_ID);
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}
	}
}
